<?php

namespace core;

use core\system\HttpRequest as HttpRequest;
use core\system\HttpResponse as HttpResponse;

class CoreBase {

    public $request = null;
    public $response = null;
    public $baseUrl = null;
    public static $imports = [];

    function __construct($arr = array('index.php', 'cp.php')) {
        $this->request = new HttpRequest();
        $this->response = new HttpResponse();
        $this->baseUrl = 'http://' . $_SERVER['HTTP_HOST'] . str_replace($arr, '', $_SERVER['SCRIPT_NAME']);
    }

    /**
     * import class
     * @param type $path
     * @param type $remove
     */
    public static function import($path = null) {
        $imports = &self::$imports;
        if (!is_array($imports)) {
            $imports = [];
        }
        $arrPath = CoreBase::readPath($path);
        foreach ($arrPath as $file) {
            if (!in_array($file, $imports)) {
                $imports[] = $file;
                require_once($file);
            }
        }
    }

    /**
     * Đọc file for path
     * @param type $path
     * @param type $remove
     * @param type $extends
     * @return type
     */
    public static function readPath($path = null, $remove = array(), $extends = '.php') {
        $arrPath = array();
        if ($path != null) {
            $current = str_replace("\\", "/", getcwd()) . "/";
            $path = $current . str_replace([".", "\\"], "/", $path);
            if (substr($path, -1) != "*") {
                $path .= $extends;
            }
            $paths = glob($path);
            if (is_array($paths) && count($paths) > 0) {
                foreach (glob($path) as $file) {
                    if ($remove != null && in_array($file, $remove))
                        continue;
                    $file = str_replace($current, "", $file);
                    if (is_dir($file))
                        import($file . ".*");
                    if (substr($file, -strlen($extends)) != $extends)
                        continue;
                    $arrPath[] = $file;
                }
            } else {
                $arrPath[] = str_replace($current, "", $path);
            }
        }
        return $arrPath;
    }

    public static function readPathFile($path = null, $remove = array()) {
        $arrPath = array();
        if ($path != null) {
            $current = str_replace("\\", "/", getcwd()) . "/";
            $path = $current . $path;
            $paths = glob($path);
            if (is_array($paths) && count($paths) > 0) {
                foreach (glob($path) as $file) {
                    if ($remove != null && in_array($file, $remove))
                        continue;
                    $file = str_replace($current, "", $file);
                    if (is_dir($file))
                        import($file . ".*");
                    $arrPath[] = $file;
                }
            } else {
                $arrPath[] = str_replace($current, "", $path);
            }
        }
        return $arrPath;
    }

}
